﻿using HualongExcel.Utilities;
using Microsoft.Office.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HualongExcel.Froms
{
    public partial class VariableWindow : UserControl
    {

        class VariableTips
        {
           public string variableName;
           public string description;
           public string howtouse;
           public string example;
        }


        List<VariableTips> variableTips = new List<VariableTips>();
        StringBuilder builder = new StringBuilder();
        public VariableWindow()
        {
            InitializeComponent();
            InitializeBuildinVariableTips();
            InitializeVariableTips();
        }

        private static CustomTaskPane Task_VariableWindow;

        #region 表格预览窗口方法
        /// <summary>
        /// 打开变量查看窗口
        /// </summary>
        public static void OpenVariableWindow()
        {

            if (Task_VariableWindow == null)
            {
                VariableWindow variableWindow = new VariableWindow();
                Task_VariableWindow = Globals.HualongExcel.CustomTaskPanes.Add(variableWindow, "变量查询");
                Task_VariableWindow.VisibleChanged += OnVariableWinodwVisibleChangeEventHandler;
                Task_VariableWindow.Width = 450;
                Task_VariableWindow.Visible = true;
            }
            else
            {
                if (!Task_VariableWindow.Visible)
                {
                    Task_VariableWindow.Visible = true;
                }
            }

        }

        /// <summary>
        /// 当变量查看窗体可见性改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnVariableWinodwVisibleChangeEventHandler(object sender, EventArgs e)
        {
            if (Task_VariableWindow.Visible)
            {
                Task_VariableWindow.Control.Show();
            }
            else
            {
                Task_VariableWindow.Control.Hide();
            }
        }

        /// <summary>
        /// 关闭变量查看窗口
        /// </summary>
        public static void CloseVariableWindow()
        {
            Globals.HualongExcel.CustomTaskPanes.Remove(Task_VariableWindow);
            Task_VariableWindow = null;
        }

        #endregion


        protected virtual void InitializeBuildinVariableTips()
        {
            variableTips.Add(new VariableTips
            {
                variableName = "Time",
                description="时间，显示今天的日期，显示的格式为：年-月-日",
                howtouse="{Time}",
                example="日期:{Time}"
              
            });
            variableTips.Add(new VariableTips
            {
                variableName = "Year",
                description = "年份，显示今年的年份",
                howtouse = "{Year}",
                example = "{Year}年"

            });
            variableTips.Add(new VariableTips
            {
                variableName = "Month",
                description = "月份，显示当前的月份",
                howtouse = "{Month}",
                example = "{Month}月"

            });
            variableTips.Add(new VariableTips
            {
                variableName = "Day",
                description = "天数，显示今天的天数",
                howtouse = "{Day}",
                example = "{Day}日"
            });
            variableTips.Add(new VariableTips
            {
                variableName = "Hour",
                description = "小时数，显示现在的小时数",
                howtouse = "{Hour}",
                example = "{Hour}时"
            });
            variableTips.Add(new VariableTips
            {
                variableName = "Minute",
                description = "分钟数，显示现在的分钟数",
                howtouse = "{Minute}",
                example = "{Minute}分"
            });
            variableTips.Add(new VariableTips
            {
                variableName = "Second",
                description = "秒数，显示现在的秒数",
                howtouse = "{Second}",
                example = "{Second}秒"
            });
            variableTips.Add(new VariableTips
            {
                variableName = "GenerateCount",
                description = "累计生成表格的次数",
                howtouse = "{GenerateCount}",
                example = "您已生成表格{GenerateCount}次"
            });
            variableTips.Add(new VariableTips
            {
                variableName = "GUID",
                description = "生成16位全局唯一标识符\n由大写字母+数字随机组成",
                howtouse = "{GUID}",
                example = "随机生成的GUID：{GUID}"
            });
            variableTips.Add(new VariableTips
            {
                variableName = "SerialNumber",
                description = "序列号，根据生成表格的次数产生的序列号\n在变量后面跟上序列号的位数",
                howtouse = "{SerialNumber[n]}或者{SerialNum[n]}\n可以生成n位的序列号，例如SerialNumber6，就可以生成6位的序列号",
                example = "生成的8位序列号为：{SerialNumber8}"
            });
            variableTips.Add(new VariableTips
            {
                variableName = "$*$*",
                description = "单元格变量，可以指定变量为当前表格中指定单元格的内容\n单元格地址一般由行号与列号组成\n列号为字母，行号为数字\n单元格内容受到替换类型的影响",
                howtouse = "{$列号$行号}",
                example = "当前替换类型下的A1单元格为：{$A$1}"
            });
        }

        private void InitializeVariableTips()
        {
            VariableListBox.Items.Clear();
            foreach (var tips in variableTips)
            {
                AddVariableTipsView(tips);
            }
        }

        private void AddVariableTipsView(VariableTips tips)
        {
            VariableListBox.Items.Add(tips.variableName);
        }

        private void VariableInputTextBox_TextChanged(object sender, EventArgs e)
        {
            VariablePreviewTextBox.Text = VariableInputTextBox.Text.ReplaceVariable();
        }

        private void VariableListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            builder.Clear();
            var selectedIndex=VariableListBox.SelectedIndex;
            if (selectedIndex < 0 || selectedIndex >= variableTips.Count)
            {
                if(TipsTextBox.Text!=String.Empty)
                    TipsTextBox.Text = String.Empty;
                return;
            }
                
            var tips=variableTips[selectedIndex];
            builder.AppendLine($"变量名称：{tips.variableName}");
            builder.AppendLine(tips.description);
            builder.AppendLine($"使用方式：{tips.howtouse}");
            builder.AppendLine($"案例：{tips.example}");
            builder.AppendLine($"结果：{tips.example.ReplaceVariable()}");
            TipsTextBox.Text = builder.ToString();
        }
    }
}
